
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yybb_flutter/Macros/yyb_macros.dart';
import 'package:yybb_flutter/http/api_response.dart';
import 'package:yybb_flutter/modules/incremental_board/models/incremtal_board_culemodel.dart';
import 'package:yybb_flutter/modules/incremental_board/vmodels/incrementai_broad_vmodel.dart';
import 'package:yybb_flutter/yybbtools/yybb_loading_Widget.dart';
import 'package:yybb_flutter/yybbtools/yybb_loading_nodata_widget.dart';
import 'package:yybb_flutter/yybbtools/yybb_services.dart';
import 'package:yybb_flutter/yybbtools/yybb_tools.dart';

class IncrementaiCulePage extends StatefulWidget {
  @override
  _IncrementaiCulePageState createState() => _IncrementaiCulePageState();
}

class _IncrementaiCulePageState extends State<IncrementaiCulePage> {
  List _dataList = [];
  var _page = 1;
  RefreshController _refreshController =  RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    _getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: YYBBColor245,
      body: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: YYBBRefreshHeadWidget(),
        footer: YYBBRefreshLoadingWidget(),
        controller: _refreshController,
        onRefresh: _onRefresh,
        onLoading: _onLoading,
        child: _childWitdget(),
      ),
    );
  }

  //  loading 加载
  Widget _childWitdget() {
    Widget _childWidget;
    if (_dataList != null && _dataList.length != 0) {
      _childWidget = ListView.builder(
        itemBuilder: _cellForRow,
        itemCount: _dataList.length > 0 ? _dataList.length : 0,
      );
    } else {
      _childWidget = YYBBNODataWidget();
      // YYBBPlatform.invokeMapMethod("NodataCallBack");
    }
    return _childWidget;
  }

  // Cell 布局
  Widget _cellForRow(BuildContext context, int index ) {
    YYBBIncrementailCuleModeResultDataList _model = _dataList[index];
    if( _model.supplier == null || _model.supplier.isEmpty){
      _model.supplier = "";
    }

    return FlatButton(
      splashColor: Colors.transparent, // 点击时的高亮效果设置为透明
      onPressed: (){
        _pushOrderDetailPage(_model);
      },
      child: Container(
        margin: EdgeInsets.fromLTRB(0, 12, 0, 0),
        constraints: BoxConstraints(
          minHeight: 10,
        ),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(4),
            ///圆角
            border: Border.all(color: Colors.white, width: 1)
          ///边框颜色、宽
        ),
        // color: Colors.white,
        child: Container(
          padding:EdgeInsets.fromLTRB(8, 12, 8, 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
           children: [
             Text(
              _model.shortName != null?_model.shortName : _model.epName,
               textAlign: TextAlign.left ,
               style: TextStyle(
                 fontSize: 16,
                 fontWeight: FontWeight.w700,
                 color: YYBBColor51,
               ),
             ),
             SizedBox(height: 7,),
             Row(
               mainAxisAlignment:MainAxisAlignment.spaceBetween,
               children: [
                 Text(
                   "所在区域   ${_model.area == null ? "":_model.area}",
                   textAlign: TextAlign.left,
                   style: TextStyle(
                     fontSize: 12,
                     color: YYBBColor119,
                   ),
                 ),
                 SizedBox(height: 8,),
                 Text(
                   "客户距离   ${_model.distance}km",
                   textAlign: TextAlign.left,
                   style: TextStyle(
                     fontSize: 12,
                     color: YYBBColor119,
                   ),
                 ),
               ],
             ),
             SizedBox(height: 7,),
             Container(
               height: 0.5,
               color: YYBBLineColor,
             ),
             SizedBox(height: 10,),
             Row(
               children: [
                 Text(
                   "总采购规模   ",
                   textAlign: TextAlign.left ,
                   style: TextStyle(
                     fontSize: 13,
                     // fontWeight: FontWeight.w200,
                     color: YYBBColor119,
                   ),
                 ),
                 Text(
                   "${_model.totalPurchaseQuantity.toString()}㎡",
                   textAlign: TextAlign.left ,
                   style: TextStyle(
                     fontSize: 13,
                     fontWeight: FontWeight.w600,
                     color: YYBBColor51,
                   ),
                 ),
               ]
             ),
             SizedBox(height: 5,),
             Row(
                 children: [
                   Text(
                     "最大主要材质克重    ",
                     textAlign: TextAlign.left ,
                     style: TextStyle(
                       fontSize: 13,
                       // fontWeight: FontWeight.w200,
                       color: YYBBColor119,             ),
                   ),
                   Text(
                     "${(_model.fluteType == null || _model.fluteType.isEmpty)? "":_model.fluteType}",
                     textAlign: TextAlign.left ,
                     style: TextStyle(
                       fontSize: 13,
                       fontWeight: FontWeight.w600,
                       color: YYBBColor51,
                     ),
                   ),
                 ]
             ),
             SizedBox(height: 5,),
             Row(
                 children: [
                   Text(
                     "最大主要材质采购量   ",
                     textAlign: TextAlign.left ,
                     style: TextStyle(
                       fontSize: 13,
                       // fontWeight: FontWeight.w200,
                       color: YYBBColor119,             ),
                   ),
                   Text(
                     "${_model.purchaseQuantit != null ? _model.purchaseQuantit : 0}万㎡",
                     textAlign: TextAlign.left ,
                     style: TextStyle(
                       fontSize: 13,
                       fontWeight: FontWeight.w600,
                       color: YYBBColor51,
                     ),
                   ),
                 ]
             ),
             SizedBox(height: 5,),
             Text(
               "最大材质供应商   ",
               textAlign: TextAlign.left ,
               style: TextStyle(
                 fontSize: 13,
                 // fontWeight: FontWeight.w200,
                 color: YYBBColor119,             ),
             ),
             SizedBox(height: 10,),
             Container(
               height: _model.supplier.isNotEmpty?22:0,
               child: FlatButton(
                 onPressed: (){},
                 shape:RoundedRectangleBorder(
                   borderRadius: BorderRadius.all(Radius.circular(3)),
                 ),
                 child: Text(
                   _model.supplier,
                   overflow: TextOverflow.ellipsis,
                   textAlign: TextAlign.justify,
                   style: TextStyle(
                     fontSize: 12.0,
                     color:Color.fromRGBO(89, 122, 191,1),
                   ),
                 ),
                 color: Color.fromRGBO (110, 136, 191,0.3),
//               color: Colors.red,
               ),
             ),
           ],
          ),
        ),
      ),
    );
  }



  // 获取数据
  void _getData() async {
    var params = Map<String, String>();
    params["currentPage"] = _page.toString();
    params["pageSize"] = "20";
    ApiResponse<YYBBIncrementailCuleModeResultData> item  = await IncrementaiBroadVmodel.getData(params);
    if(_page == 1) {
      _dataList.clear();
    }
    _dataList.addAll(item.data.list);
    _stopRefresh();
    // // 刷新界面
    setState(() {});
  }


  // 点击进入下一个界面
  void _pushOrderDetailPage(YYBBIncrementailCuleModeResultDataList model){
    var params = Map<String, String>();
    params["customerId"] = model.customerId.toString();
    params["enterpriseId"] = model.enterpriseId.toString();
    params["sellerEnterpriseId"] = model.sellerEnterpriseId.toString();
    YYBBPlatform.invokeMethod("jumpCustomerPortraitVC", params);

   }

  //下拉加载
  void _onRefresh() async{
    // await Future.delayed(Duration(milliseconds: 1000));
    _page = 1;
    _getData();
    setState(() {

    });

  }

  //上拉加载
  void _onLoading() async{
    // monitor network fetch
    // Rawait Future.delayed(Duration(milliseconds: 1000));
    _page++;
    _getData();
    if(mounted)
      setState(() {
      });
  }


  // 停止刷新
  void _stopRefresh(){

    if(_dataList.length  < 10 * _page){
      _refreshController.loadNoData();
    }else{
      _refreshController.loadComplete();
    }
    _refreshController.refreshCompleted();
  }

}
